#!/usr/bin/python3.8.9
# -*- coding: utf-8 -*-

# def insert_register_data():
#     conn = pymysql.connect(host='localhost', port=3306, user='root',passwd='123456', db='tina')
#     cursor = conn.cursor()
#     cursor.execute("INSERT into test0 (item1,item3)VALUES('11','333');")
#     conn.commit()
#     conn.close()
#     cursor.close()l

# @Author  : Tina Yu
# @Time    : 2022-11-6 11:49
import datetime
import time

import pymysql
import shortuuid
from faker import Faker

from lingzhu.tools import get_run_time


class DatabaseAcess:
    # 初始化属性(包括host、port、user、password和database）
    def __init__(self):
        self.__db_host = 'localhost'
        self.__db_port = 3306
        self.__db_user = 'root'
        self.__db_password = '123456'
        self.__db_database = 'cifdb'

    # 连接数据库
    def isConnection(self):
        self.__db = pymysql.connect(
            host=self.__db_host,
            port=self.__db_port,
            user=self.__db_user,
            password=self.__db_password,
            database=self.__db_database,
            charset='utf8'
        )

    # 插入数据进pay表
    def insert_register_data(self, register_value_list):
        # 连接数据库
        self.isConnection()
        # 创建游标
        cursor = self.__db.cursor()
        # 执行
        cursor.executemany(
            'INSERT INTO `t_customer_base` (id,customer_name,mobile_phone,apply_loan_result,apply_bt_result,'
            'apply_consume_result,bank_depository_status,modify_date,create_date,issue_channel,regist_channel,'
            'risk_level,`STATUS`,depository_bind_card_status)values'
            '( % s, % s, % s, % s, % s, % s, % s, % s, % s, % s, % s, % s, % s, % s);',
            register_value_list)
        cursor.close()
        self.__db.commit()
        self.__db.close()

        # 组装数据

    def sigle_register_data(self):
        fake = Faker(locale='zh_CN')
        register_data_list = []
        data_id = f"{datetime.date.today().strftime('%Y%m%d')}" \
                  f"{shortuuid.ShortUUID(alphabet='1234567890').random(length=6)}" \
                  f"{datetime.datetime.now().strftime('%H%M%S')}"
        customer_name = 'Tina插入数据'
        mobile_phone = fake.phone_number()
        apply_loan_result = 'PRE'
        apply_bt_result = 'PRE'
        apply_consume_result = 'PRE'
        bank_depository_status = 'UNREGISTERED'
        modify_date = datetime.datetime.now()
        create_date = datetime.datetime.now()
        issue_channel = 'ANDROID'
        regist_channel = 'ANDROID'
        risk_level = 'A'
        status = 'OPEN'
        depository_bind_card_status = 'NOBINDCARD'

        register_data_list.append(data_id)
        register_data_list.append(customer_name)
        register_data_list.append(mobile_phone)
        register_data_list.append(apply_loan_result)
        register_data_list.append(apply_bt_result)
        register_data_list.append(apply_consume_result)
        register_data_list.append(bank_depository_status)
        register_data_list.append(modify_date)
        register_data_list.append(create_date)
        register_data_list.append(issue_channel)
        register_data_list.append(regist_channel)
        register_data_list.append(risk_level)
        register_data_list.append(status)
        register_data_list.append(depository_bind_card_status)

        return register_data_list

    # 生成数据列表，并调用sql
    def batch_data(self, batch_num):
        batch_data_list = []

        for i in range(0, batch_num):
            sigle_data_list = db.sigle_register_data()
            batch_data_list.append(sigle_data_list)

        return batch_data_list


if __name__ == '__main__':
    db = DatabaseAcess()
    batch_num = 100
    batch_times = 2000
    times = 0
    start_time = time.time()
    for i in range(0, batch_times):
        db.insert_register_data(db.batch_data(batch_num))
        times += 1
        print(f'第{times}次插入{batch_num}条数据！')
    end_time = time.time()
    print(f"共计插入{batch_times * batch_num}条数据!")
    get_run_time.get_run_time(end_time, start_time)
